package com.jdrx.sw.sewage.api.web;

import com.jdrx.platform.common.support.gateway.GwConstants;
import com.jdrx.platform.commons.rest.beans.dto.IdDTO;
import com.jdrx.platform.commons.rest.beans.vo.ResposeVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory;
import com.jdrx.sw.sewage.beans.dto.DictClassifyQueryDTO;
import com.jdrx.sw.sewage.beans.dto.DictClassifysQueryDTO;
import com.jdrx.sw.sewage.beans.dto.DictQueryDTO;
import com.jdrx.sw.sewage.beans.dto.base.IdsDTO;
import com.jdrx.sw.sewage.beans.vo.DictVO;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.service.DictClassifyServiceImpl;
import com.jdrx.sw.sewage.service.DictServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_USERID_FEILD;

@RestController
@RequestMapping(value = "/api/dict")
@Api(value = "字典表模块")
public class DictApi {


    @Autowired
    DictServiceImpl dictServiceImpl;

    @Autowired
    DictClassifyServiceImpl dictClassifyServiceImpl;

    @ApiOperation(value = "新建字典表")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public ResposeVO create(@RequestHeader(TRANSPARENT_USERID_FEILD) Long createBy,
                            @RequestHeader(Constant.DATA_NODE) String dataNode,
                            @RequestHeader(Constant.TENANT_ID) String tenantId,
                            @RequestHeader(value = GwConstants.TRANSPARENT_TOKEN_FEILD) String token,
                            @RequestBody DictVO vo) throws BizException {
        dictServiceImpl.save(vo, createBy, token);
        return ResponseFactory.ok("创建成功");
    }


    @ApiOperation(value = "字典表分页查询")
    @RequestMapping(value = "selectByPage", method = RequestMethod.POST)
    public ResposeVO selectByPage(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                  @RequestHeader(Constant.TENANT_ID) String tenantId,
                                  @RequestBody DictQueryDTO dto) throws BizException {
        return ResponseFactory.ok(dictServiceImpl.selectByPage(dto));
    }

    @ApiOperation(value = "字典表按照分类Id查询")
    @RequestMapping(value = "selectWithClassifyId", method = RequestMethod.POST)
    public ResposeVO selectWithClassifyId(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                          @RequestHeader(Constant.TENANT_ID) String tenantId,
                                          @RequestBody IdDTO dto) throws BizException {
        return ResponseFactory.ok(dictServiceImpl.selectWithClassifyId(dto));
    }

    @ApiOperation(value = "字典表按照分类ids查询")
    @RequestMapping(value = "selectWithClassifyIds", method = RequestMethod.POST)
    public ResposeVO selectWithClassifyIds(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                           @RequestHeader(Constant.TENANT_ID) String tenantId,
                                           @RequestBody IdsDTO dto) throws BizException {
        return ResponseFactory.ok(dictServiceImpl.selectWithClassifyIds(dto));
    }

    @ApiOperation(value = "字典表按照分类查询")
    @RequestMapping(value = "selectWithClassifyKey", method = RequestMethod.POST)
    public ResposeVO selectWithClassifyKey(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                           @RequestHeader(Constant.TENANT_ID) String tenantId,
                                           @RequestBody DictClassifyQueryDTO dto) throws BizException {
        return ResponseFactory.ok(dictServiceImpl.selectWithClassifyKey(dto));
    }

    @ApiOperation(value = "字典表按照分类查询")
    @RequestMapping(value = "selectWithClassifyKeys", method = RequestMethod.POST)
    public ResposeVO selectWithClassifyKeys(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                            @RequestHeader(Constant.TENANT_ID) String tenantId,
                                            @RequestBody DictClassifysQueryDTO dto) throws BizException {
        return ResponseFactory.ok(dictServiceImpl.selectWithClassifyKeys(dto));
    }


    @ApiOperation(value = "修改字典表")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ResposeVO update(@RequestHeader(TRANSPARENT_USERID_FEILD) Long createBy,
                            @RequestHeader(Constant.DATA_NODE) String dataNode,
                            @RequestHeader(Constant.TENANT_ID) String tenantId,
                            @RequestHeader(value = GwConstants.TRANSPARENT_TOKEN_FEILD) String token,
                            @RequestBody DictVO vo) throws BizException {
        dictServiceImpl.update(vo, createBy, token);
        return ResponseFactory.ok("修改成功");
    }

    @ApiOperation(value = "根据id查询字典表")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ResposeVO getById(@RequestHeader(Constant.DATA_NODE) String dataNode,
                             @RequestHeader(Constant.TENANT_ID) String tenantId,
                             @RequestBody IdDTO<Long> idDTO) throws BizException {
        return ResponseFactory.ok(dictServiceImpl.getById(idDTO));
    }

    @ApiOperation(value = "删除字典表")
    @RequestMapping(value = "deleteById", method = RequestMethod.POST)
    public ResposeVO delete(@RequestHeader(TRANSPARENT_USERID_FEILD) Long createBy,
                            @RequestHeader(Constant.DATA_NODE) String dataNode,
                            @RequestHeader(Constant.TENANT_ID) String tenantId,
                            @RequestBody IdDTO<Long> idDTO) throws BizException {
        dictServiceImpl.delete(idDTO, createBy);
        return ResponseFactory.ok("删除成功");
    }

    @ApiOperation(value = "查询所有字典分类")
    @RequestMapping(value = "selectAllClassify", method = RequestMethod.POST)
    public ResposeVO selectAllClassify(@RequestHeader(Constant.DATA_NODE) String dataNode,
                                       @RequestHeader(Constant.TENANT_ID) String tenantId) throws BizException {
        dictClassifyServiceImpl.findAll();
        return ResponseFactory.ok(dictClassifyServiceImpl.findAll());
    }

}
